package com.example.service;

import com.example.entity.Message;
import com.example.mapper.MessageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class MessageService {
    // 自动注入MessageMapper
    @Resource
    private MessageMapper messageMapper;

    // 创建新消息

    public int createMessage(Message message) {
        return messageMapper.insert(message);
    }

    // 更新消息

    public int updateMessage(Message message) {
        return messageMapper.update(message);
    }

    // 删除消息

    public int deleteMessage(Integer mid) {
        return messageMapper.deleteById(mid);
    }

    // 根据ID获取消息

    public Message getMessageById(Integer mid) {
        return messageMapper.selectById(mid);
    }

    // 获取所有消息

    public List<Message> getAllMessages() {
        return messageMapper.selectAll();
    }

    /**
     * 分页查询消息
     * @param message 查询条件
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页结果
     */

    public PageInfo<Message> selectPage(Message message, Integer pageNum, Integer pageSize) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 执行查询
        List<Message> messageList = messageMapper.selectAll();
        // 返回分页信息
        return new PageInfo<>(messageList);
    }


    public List<Message> getMessagesByReceiver(Integer uid) {
        return messageMapper.selectByReceiver(uid);
    }


    public List<Message> getMessagesBySender(Integer uid) {
        return messageMapper.selectBySender(uid);
    }


    public List<Message> getUserMessages(Integer uid) {
        return messageMapper.selectByUser(uid);
    }
}
